//
//  SiftView.m
//  筛选框
//
//  Created by songhuipan on 16/6/26.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

#import "SiftView.h"
#import "UIView+Frame.h"
static NSString *const siftCellId = @"siftCellId";
@interface SiftView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,assign)NSInteger count;
@property (nonatomic,strong)UIView *maskView;
@end
@implementation SiftView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
       dispatch_async(dispatch_get_main_queue(), ^{
           [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:siftCellId];
           [self addSubview:self.maskView];
           [self addSubview:self.tableView];
       });
    }
    return self;
}
- (void)setIsScrollToTop:(NSInteger)isScrollToTop {
    _isScrollToTop = isScrollToTop;
    if (isScrollToTop) {
        [UIView animateWithDuration:0.25 animations:^{
            self.h = 0;
            self.tableView.h = 0;
            self.maskView.h = 0;
        }];
    }else {
        [UIView animateWithDuration:0.25 animations:^{
            self.h = 44;
            self.tableView.h = 44;
            self.maskView.h = 44;
        }];
    }
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        UIButton *headerButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.w, 44)];
        [headerButton setTitle:@"筛选" forState:UIControlStateNormal];
        [headerButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [headerButton addTarget:self action:@selector(headerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _tableView.tableHeaderView = headerButton;
    }
    return _tableView;
}
- (void)headerButtonClick:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter]postNotificationName:@"siftViewChangeHeight" object:nil];
    self.tableView.h = self.h*0.5;
    self.maskView.h = self.h;
    self.count = self.count?0:5;
    [self.tableView reloadData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:siftCellId];
    cell.textLabel.text = [NSString stringWithFormat:@"筛选%ld",indexPath.row];
    return cell;
}
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc]initWithFrame:self.bounds];
        _maskView.backgroundColor = [UIColor blackColor];
        _maskView.alpha = 0.3;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removemaskView:)];
        [_maskView addGestureRecognizer:tap];
    }
    return _maskView;
}
- (void)removemaskView:(UIGestureRecognizer *)tap {
    [UIView animateWithDuration:0.25 animations:^{
        self.h = 44;
        self.maskView.h = 44;
        self.tableView.h = 44;
        self.count = 0;
        [self.tableView reloadData];
    }];
}
@end




























